package com.lf.lexy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.lf.comm.MyConstants;
import com.lf.lexy.business.BlogBusiness;
import com.lf.lexy.dao.UserDao;
import com.lf.lexy.model.BlogContent;
import com.lf.lexy.model.User;
import com.lf.lexy.model.dto.ResponeObj;
import com.lf.lexy.webutils.CommMethod;


public class IndexController extends BaseController {
	
	
	private BlogBusiness blogBusiness = new BlogBusiness();

	public void index(){
		List<Record> blogTypeList = blogBusiness.getBlogTypeList("999999");
		setAttr("blogTypeList", blogTypeList);
		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList("999999" , "" , "1" ,MyConstants.PAGE_SIZE_REC);
		setAttr("bloglist", blogContentList);
		setAttr("enFlag", MyConstants.ENFLAG);
//		render("rec_index.jsp");
		render("rec_index.jsp");
	}
	
	
	public void index_1(){
		List<Record> blogTypeList = blogBusiness.getBlogTypeList("999999");
		setAttr("blogTypeList", blogTypeList);
		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList("999999" , "" , "1" ,MyConstants.PAGE_SIZE_REC);
		setAttr("bloglist", blogContentList);
		setAttr("enFlag", MyConstants.ENFLAG);
////		render("rec_index.jsp");
		render("rec_index.jsp");
	}
	
	
	
	
	
	
	
	public void signIn(){
		
		ResponeObj obj = new ResponeObj();
		Map<String , Object> map = new HashMap<String , Object>();
		
		String par = super.getPar("reqStr");
		HashMap<String, String> parseObject = MyConstants.json.parseObject(par , new HashMap<String, String>().getClass());
		String userName = parseObject.get("name");
		String userPwd = parseObject.get("pwd");
		User user = UserDao.me.getUser(userName, userPwd);
		if(user==null){
			obj.setCode("000");
			obj.setErrMsg("不存在该用户!");
			super.success(obj);
			return;
		}
		if(user!=null){
			String isDelete = user.getIsDelete();
			if(MyConstants.IS_DELETE_Y.equals(isDelete)){
				obj.setCode("000");
				obj.setErrMsg("该用户被禁止登录,请联系管理员!");
				super.success(obj);
				return;
			}else{
				setSessionAttr(MyConstants.SESSION_USER, user);
				obj.setCode("200");
				obj.setErrMsg("登录成功！");
				super.success(obj);
				return;
			}
		}
	}
	
	
	
	public void sginOut(){
		getSession().removeAttribute("user");
		Map<String, Object> map = CommMethod.getMap();
		map.put("code", "200");
		super.success(map);
	}

	
	
	
	
	
	
	
	
	
	
	
	@SuppressWarnings("static-access")
	public void getBlogContentPageInint(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		if(user==null){
			super.success("请先登录在操作！");
			return;
		}
		String reqStr = getPara("reqStr");
		
		@SuppressWarnings("unchecked")
		Map<String , String> parseObject = (Map<String , String>)MyConstants.json.parseObject(reqStr, Map.class);
		if(parseObject==null || parseObject.size()<1){
			super.success("请求参数不能为空！！");
			return;
		}
		
		String pageNum = parseObject.get("pageNum");
		String typeNo = parseObject.get("typeNo");
		
		Map<String, Object> map = CommMethod.getMap();
		if(StringUtils.isEmpty(pageNum)){
			super.success("请求参数不能为空！");
			return;
			
		}
		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList(user.getUserNo() , typeNo , pageNum ,MyConstants.PAGE_SIZE_REC);
		map.put("bloglist", blogContentList);
		super.success(map);
		
	}
	
	
	
	
	

	
	
	
	

}
